import { deepAssign } from '@/base/util/Objects';
import { Page } from '@/base/Page';
import { DeviceDistibuteVo } from '@/base/model/DeviceDistibuteVo';
import { DeviceUse } from '@/base/model/DeviceUse';
import { ModeLifetimeVo } from '@/base/model/ModeLifetimeVo';
import { ModeUseNumVo } from '@/base/model/ModeUseNumVo';
import { getDeviceUseList, getDeviceUseModelLifetime, getDeviceUseModeUse } from '@/base/api/DeviceUseApi';
import {
  getDevcieDistribute, getDeviceStatusNum
} from '@/base/api/DevcieApi';
import { getDeviceFaultNum } from '@/base/api/DeviceFaultApi';
import { DeviceFaultVo } from '@/base/model/DeviceFaultVo';
import { DeviceStatusVo } from '@/base/model/DeviceStatusVo';

export class DataBloc {
  loading = false;
  DeviceDistribute: Page<DeviceDistibuteVo> = {
    extras: [],
    itemCount: 0,
    items: [],
    pageCount: 0,
    pageSize: 10,
    pageIndex: 1
  };
  DeviceUse: Page<DeviceUse> = {
    extras: [],
    itemCount: 0,
    items: [],
    pageCount: 0,
    pageSize: 10,
    pageIndex: 1
  };
  model: any = {
    companyId: 0 || undefined,
    createBegin: 0 || undefined,
    createEnd: 0 || undefined
  };
  ModeLifetimeVo: {} = {
    'lifetime': 0,
    'modename': '',
    'selfmotionid': 0
  };
  ModeUseNumVo: {} = {
    'modename': '',
    'num': 0,
    'selfmotionid': 0
  };
  num: object = {
    xAxis: [],
    yAxis: [],
    text: `故障统计：`
  }
  statusNum: object = {
    xAxis: [],
    yAxis: [],
    text: `设备状态统计：`
  }
  modelTime: object = {
    xAxis: [],
    yAxis: [],
    text: `模式使用时长：`
  }
  modelNum: object = {
    xAxis: [],
    yAxis: [],
    text: `模式使用次数：`
  }

  modeUseNumVo: Array<ModeUseNumVo> = []
  deviceStatusNum: Array<DeviceStatusVo> = []
  modeLifetimeVo: Array<ModeLifetimeVo> = []
  deviceFaultVo: Array<DeviceFaultVo> = []

  getDevcieDistribute(pageIndex, pageSize, model) {
    this.loading = true;
    return getDevcieDistribute({
      pageIndex,
      pageSize,
      sortDirection: 'ASC',
      companyId: model.companyId,
      createBegin: model.createBegin,
      createEnd: model.createEnd
    }).then((res) => {
      deepAssign(this.DeviceDistribute, res.data);
      return res;
    }).finally(() => {
      this.loading = false;
    });
  }

  //设备使用次数
  getDeviceUse(pageIndex, pageSize, sortDirection) {
    return getDeviceUseList({
      pageIndex,
      pageSize,
      sortDirection
    }).then(res => {
      deepAssign(this.DeviceUse, res.data)
      return res;
    }).finally(() => {

    })
  }

  //设备使用模式时长
  getDeviceUseModelLifetime(deviceId, companyId, deviceModelId) {
    return getDeviceUseModelLifetime({ deviceId, companyId, deviceModelId }).then(res => {
      this.modeLifetimeVo = res.data
      this.modelTime['xAxis'] = this.modeLifetimeVo.map(item => item.modename)
      this.modelTime['yAxis'] = this.modeLifetimeVo.map(item => item.lifetime)
    }).finally(() => {

    })
  }

  //设备状态统计
  getDeviceStatusNum(deviceModelId) {
    return getDeviceStatusNum({ deviceModelId }).then(res => {
      this.deviceStatusNum = res.data
      this.statusNum['xAxis'] = this.deviceStatusNum.map(item => item.statusmeg)
      this.statusNum['yAxis'] = this.deviceStatusNum.map(item => item.num)
    }).finally(() => {

    })
  }

  //设备使用模式次数
  getDeviceUseModeUse(deviceId, companyId, deviceModelId) {
    return getDeviceUseModeUse({ deviceId, companyId, deviceModelId }).then(res => {
      this.modeUseNumVo = res.data
      this.modelNum['xAxis'] = this.modeUseNumVo.map(item => item.modename)
      this.modelNum['yAxis'] = this.modeUseNumVo.map(item => item.num)
    }).finally(() => {

    })
  }

  //设备故障数量
  getDeviceFaultNum(deviceModelId) {
    return getDeviceFaultNum({ deviceModelId }).then(res => {
      this.deviceFaultVo = res.data
      this.num['xAxis'] = this.deviceFaultVo.map(item => item.typemeg)
      this.num['yAxis'] = this.deviceFaultVo.map(item => item.num)
    }).finally(() => {

    })
  }


}
